package com.canbot.u05.activity.shoppingGuide.codeMarketing;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.canbot.u05.R;
import com.canbot.u05.adapter.BasePageAdapter;
import com.canbot.u05.bean.codeMarketing.CodeMarketingInfo;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.utils.qr.QRCodeTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodePreviewActivity extends BaseCodeFinishActivity {

        @BindView(R.id.code_bg_vp)
        ViewPager codeBgVp;

        @BindView(R.id.code_iv_back)
        Button codeIvBack;

        @BindView(R.id.code_iv_1)
        ImageView codeIv1;

        @BindView(R.id.code_tv_1)
        TextView codeTv1;

        @BindView(R.id.code_ll_1)
        LinearLayout codeLl1;

        @BindView(R.id.code_iv_2)
        ImageView codeIv2;

        @BindView(R.id.code_tv_2)
        TextView codeTv2;

        @BindView(R.id.code_ll_2)
        LinearLayout codeLl2;

        @BindView(R.id.code_iv_3)
        ImageView codeIv3;

        @BindView(R.id.code_tv_3)
        TextView codeTv3;

        @BindView(R.id.code_ll_3)
        LinearLayout codeLl3;

        @BindView(R.id.code_main_rl)
        RelativeLayout codeMainRl;

        @BindView(R.id.code_pre_up_iv)
        ImageView codePreUpIv;

        @BindView(R.id.code_pre_next_iv)
        ImageView codePreNextIv;

        @BindView(R.id.code_iv_save)
        Button codeIvSave;

        @BindView(R.id.tv_title)
        TextView tvTitle;

        @BindView(R.id.hint_view)
        View hintView;

        private int currentPosition;

        private PopupWindow popupWindow;

        private ToastTools toastTools;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_code_preview);
                ButterKnife.bind(this);
                toastTools = ToastTools.newInstance(this);
                initView();
                initData();
                MapVoiceAndEyes.CODE_MARKETING[(int) (Math.random() * 2) + 8].playMapVoiceAndEyes();
        }

        private void initData() {
                CodeMarketingInfo marketingInfo = CodeDataHelper.getInstance().getCodeMarketingInfo();
                String title = marketingInfo.getTitle();
                tvTitle.setText(title);
                setCode(marketingInfo);
                codePreNextIv.post(new Runnable() {
                        @Override
                        public void run() {
                                showPopupWindow();
                        }
                });
        }

        private void initView() {
                tvTitle.setSelected(true);
                codeBgVp.setAdapter(new BasePageAdapter(list) {
                        @Override
                        public void setPageView(View inflate, int position, int layoutID, Object o) {
                                ImageView imageView = (ImageView) inflate.findViewById(R.id.imageView);
                                Glide.with(CodePreviewActivity.this)
                                     .load(CodeDataHelper.CODE_DRAWS.get(position))
                                     .into(imageView);
                        }
                });
                codeBgVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
                                currentPosition = position;
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {

                        }
                });
        }

        private List<Integer> list = new ArrayList<>();

        {
                list.add(R.layout.code_bg_item_iv);
                list.add(R.layout.code_bg_item_iv);
                list.add(R.layout.code_bg_item_iv);
                list.add(R.layout.code_bg_item_iv);
                list.add(R.layout.code_bg_item_iv);
                list.add(R.layout.code_bg_item_iv);
                list.add(R.layout.code_bg_item_iv);
                list.add(R.layout.code_bg_item_iv);
        }


        @Override
        protected void onResume() {
                super.onResume();
        }

        @OnClick({R.id.code_pre_up_iv, R.id.code_pre_next_iv, R.id.code_iv_back, R.id.code_iv_save})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.code_pre_up_iv:
                                currentPosition--;
                                if (currentPosition < 0) {
                                        currentPosition = 0;
                                }
                                codeBgVp.setCurrentItem(currentPosition, true);
                                break;
                        case R.id.code_pre_next_iv:
                                currentPosition++;
                                if (currentPosition > CodeDataHelper.CODE_DRAWS.size() - 1) {
                                        currentPosition = CodeDataHelper.CODE_DRAWS.size() - 1;
                                }
                                codeBgVp.setCurrentItem(currentPosition, true);
                                break;
                        case R.id.code_iv_back:
                                finish();
                                break;
                        case R.id.code_iv_save:
                                CodeDataHelper.getInstance().getCodeMarketingInfo().setBgResourcesIndex(currentPosition);
                                upMarketingData();
                                break;
                }
        }
        private Dialog dialogLoading;
        private void upMarketingData() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                CodeMarketingInfo info = CodeDataHelper.getInstance().getCodeMarketingInfo();
                String url = IpMsgConst.HTTP_URL_CHEST + "codeMarketingAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 0);
                hashMap.put("data", JSON.toJSONString(info));
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                CodeDataHelper.getInstance().saveData();
                                sendFinishCodeBroadcast();
                        }

                        @Override
                        public void onFailure() {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.service_error_hint));
                        }
                });
        }



        private void showPopupWindow() {
                if (popupWindow == null) {
                        View inflate = View.inflate(this, R.layout.code_bg_hint_popu, null);
                        inflate.findViewById(R.id.code_bg_next_tv).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                        popupWindow.dismiss();
                                        codePreNextIv.performClick();
                                }
                        });
                        popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                        popupWindow.setOutsideTouchable(true); //设置 popupWindow外面可点击
                        popupWindow.setBackgroundDrawable(new ColorDrawable());
                        //给popupWindow设置一个背景
                        //                popupWindow.setFocusable(true); //可以获取焦点
                        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                                @Override
                                public void onDismiss() {
                                        hintView.setVisibility(View.GONE);
                                }
                        });
                }
                int[] location = new int[2];
                int width = popupWindow.getContentView().getMeasuredWidth();
                codePreNextIv.getLocationInWindow(location);
                int setWidth = codePreNextIv.getWidth();
                popupWindow.showAtLocation(codeMainRl, Gravity.START | Gravity.TOP, location[0] - width + setWidth- 10, location[1]);
        }

        private void setCode(CodeMarketingInfo info) {
                String codeTitle1 = info.getCodeInfo1().getCodeTitle();
                String codeData1 = info.getCodeInfo1().getCodeData();
                String codeTitle2 = info.getCodeInfo2().getCodeTitle();
                String codeData2 = info.getCodeInfo2().getCodeData();
                String codeTitle3 = info.getCodeInfo3().getCodeTitle();
                String codeData3 = info.getCodeInfo3().getCodeData();
                switch (info.getType()) {
                        case 1:
                                codeLl1.setVisibility(View.VISIBLE);
                                Bitmap resource = BitmapFactory.decodeResource(getResources(), R.drawable.uu_logo_code);
                                Bitmap image = QRCodeTools.createImage(codeData1, 400, 400, resource);
                                codeIv1.setImageBitmap(image);
                                codeTv1.setText(codeTitle1);
                                break;
                        case 2:
                                codeLl1.setVisibility(View.VISIBLE);
                                codeLl2.setVisibility(View.VISIBLE);
                                resource = BitmapFactory.decodeResource(getResources(), R.drawable.uu_logo_code);
                                image = QRCodeTools.createImage(codeData1, 400, 400, resource);
                                codeIv1.setImageBitmap(image);
                                codeTv1.setText(codeTitle1);
                                image = QRCodeTools.createImage(codeData2, 400, 400, resource);
                                codeIv2.setImageBitmap(image);
                                codeTv2.setText(codeTitle2);
                                break;
                        case 3:
                                codeLl1.setVisibility(View.VISIBLE);
                                codeLl2.setVisibility(View.VISIBLE);
                                codeLl3.setVisibility(View.VISIBLE);
                                resource = BitmapFactory.decodeResource(getResources(), R.drawable.uu_logo_code);
                                image = QRCodeTools.createImage(codeData1, 400, 400, resource);
                                codeIv1.setImageBitmap(image);
                                codeTv1.setText(codeTitle1);
                                image = QRCodeTools.createImage(codeData2, 400, 400, resource);
                                codeIv2.setImageBitmap(image);
                                codeTv2.setText(codeTitle2);
                                image = QRCodeTools.createImage(codeData3, 400, 400, resource);
                                codeIv3.setImageBitmap(image);
                                codeTv3.setText(codeTitle3);
                                break;
                }
        }
}
